const state = {
  netRow: JSON.parse(sessionStorage.getItem("setNetRow")) || "",
  siteRow: JSON.parse(sessionStorage.getItem("setSiteRow")) || "",
  columnRow: JSON.parse(sessionStorage.getItem("setColumnRow")) || "",
  userGroupRow: JSON.parse(sessionStorage.getItem("userGroupRow")) || "",
  publicTaskIds: JSON.parse(sessionStorage.getItem("publicTaskIds")) || [],
};
const mutations = {
  SET_NETROW: (state, netRow) => {
    state.netRow = netRow;
    sessionStorage.clear();
    sessionStorage.setItem("setNetRow", JSON.stringify(netRow));
  },
  SET_SITEROW: (state, siteRow) => {
    state.siteRow = siteRow;

    sessionStorage.setItem("setSiteRow", JSON.stringify(siteRow));

    // sessionStorage.removeItem("setColumnRow");
    // sessionStorage.removeItem("userGroupRow");
  },
  SET_COLUMNROW: (state, columnRow) => {
    state.columnRow = columnRow;

    sessionStorage.setItem("setColumnRow", JSON.stringify(columnRow));
  },
  SET_USERGROUPROW: (state, userGroupRow) => {
    state.userGroupRow = userGroupRow;

    sessionStorage.setItem("userGroupRow", JSON.stringify(userGroupRow));
  },
  // 新增发布taskid
  ADD_PUBLIC_TASKID: (state, publicTaskId) => {
    if (state.publicTaskIds.some((id) => id === publicTaskId)) return;
    state.publicTaskIds.push(publicTaskId);
    sessionStorage.setItem(
      "publicTaskIds",
      JSON.stringify(state.publicTaskIds)
    );
  },
  // 发布完成后删除taskid
  DEL_PUBLIC_TASKID: (state, publicTaskId) => {
    for (let index = 0; index < state.publicTaskIds.length; index++) {
      const taskId = state.publicTaskIds[index];
      if (taskId === publicTaskId) {
        state.publicTaskIds.splice(index, 1);
        break;
      }
    }

    sessionStorage.setItem(
      "publicTaskIds",
      JSON.stringify(state.publicTaskIds)
    );
  },
};

const actions = {
  // 修改netRow
  setNetRow({ commit }, netRow) {
    commit("SET_NETROW", netRow);
  },
  // 修改siteRow
  setSiteRow({ commit }, siteRow) {
    commit("SET_SITEROW", siteRow);
  },
  // 修改columnRow
  setColumnRow({ commit }, columnRow) {
    commit("SET_COLUMNROW", columnRow);
  },
  // 修改userGroupRow
  setUserGroupRow({ commit }, userGroupRow) {
    commit("SET_USERGROUPROW", userGroupRow);
  },

  addPublicTaskId({ commit }, publicTaskId) {
    commit("ADD_PUBLIC_TASKID", publicTaskId);
  },
  delPublicTaskId({ commit, state }, publicTaskId) {
    commit("DEL_PUBLIC_TASKID", publicTaskId);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
